package pl.nith.battleships.controllers.listeners;

import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

import pl.nith.battleships.helpers.TileHelper;
import pl.nith.battleships.helpers.TileHelper.TileType;
import pl.nith.battleships.managers.GameStateManager.GameState;
import pl.nith.battleships.managers.GameStateManager.Turn;
import pl.nith.battleships.managers.Manager;
import pl.nith.battleships.models.Tile;
import pl.nith.battleships.windows.panels.BoardSurface;

public class MouseListener implements java.awt.event.MouseListener, MouseMotionListener
{	
	@Override
	public void mouseDragged(MouseEvent e)
	{
		// TODO Auto-generated method stub	
	}

	@Override
	public void mouseMoved(MouseEvent e)
	{		
		BoardSurface.mousePos = e.getPoint();
		
		if (BoardSurface.mousePos.x > 351 && BoardSurface.mousePos.x < 512)
			BoardSurface.mouseOverBar = true;
		else
			BoardSurface.mouseOverBar = false;
	}

	@Override
	public void mouseClicked(MouseEvent e)
	{		
		Tile tile = TileHelper.getTileFromCoords(e.getPoint().x, e.getPoint().y);
		
		if (Manager.getGMS().turn == Turn.MY_TURN)
		{
			if (tile.isOnHitBoard() && tile.getType() == TileType.EMPTY)
			{
				String field = TileHelper.getFieldFromTile(tile.getRow(), tile.getColumn());
				
				if (Manager.getServer().isServerRunning)
					Manager.getServer().sendMessage("ID1", "FIELD_" + field);
				
				if (Manager.getClient().isClientConnected)
					Manager.getClient().sendMessage("FIELD_" + field);
				
				Manager.getBoard().setTile(tile.getRow(), tile.getColumn(), TileType.HIT, true);
			}		
		}
		else
			if (Manager.getGMS().currentGameState == GameState.SETTING_SHIPS)
				Manager.getBoard().setTile(tile.getRow(), tile.getColumn(), TileType.HIT, false);
	}

	@Override
	public void mouseEntered(MouseEvent e)
	{
		BoardSurface.mouseOverBoard = true;
	}

	@Override
	public void mouseExited(MouseEvent e)
	{
		BoardSurface.mouseOverBoard = false;
	}

	@Override
	public void mousePressed(MouseEvent e)
	{
		// TODO Auto-generated method stub	
	}

	@Override
	public void mouseReleased(MouseEvent e)
	{
		// TODO Auto-generated method stub	
	}
}
